<?php
namespace Modules\Admin\Controllers\Ucenter;

use S\Request;
use Base\Exception\Controller as Exception;
use Modules\Admin\Model\Dao\Db\User\User as DbUser;
use Modules\Admin\Model\Data\User as DataUser;

/**
 * @用户管理
 */
class User extends Controller {

    protected $sys_view = true;

    /**
     * @name 编辑个人信息
     */
    public function userInfoPopAction() {
        $data_user = new DataUser();
        $params = $data_user->getUserInfoById(Request::session('uid'));

        $this->setResponseFormat(\S\Response::FORMAT_PLAIN);
        $this->response = $this->getRenderView("", $params);
    }

    /**
     * @name 编辑个人密码
     */
    public function passwdPopAction() {
        $this->setResponseFormat(\S\Response::FORMAT_PLAIN);
        $this->response = $this->getRenderView();
    }

    /**
     * @name 编辑用户信息
     */
    public function usermgrPopAction() {
        $uid = intval($this->getParams('uid'));
        $params = (new DataUser())->getUserInfo($uid);

        $this->setResponseFormat(\S\Response::FORMAT_PLAIN);
        $this->response = $this->getRenderView("", $params);
    }
    
    /**
     * @name 保存个人信息
     * @throws Exception
     */
    public function modifyUserInfoAction() {
        $phone = $this->getParams('phone');
        $nick = $this->getParams('nick');

		if(!preg_match('/^1[345789]\d{9}$/', $phone)) {
            throw new Exception('error.admin.phone_number_is_wrong');
		}
        if(!$nick || strlen($nick) > 30){
            throw new Exception('error.admin.name_is_wrong');
        }

        (new DataUser())->saveUserInfo(array('phone'=>$phone, 'nick'=>$nick), Request::session('uid'));
        $this->response['msg'] = '信息保存成功';
    }

    /**
     * @name 重置个人密码
     * @throws Exception
     */
    public function resetPassWordAction() {
        $passold = $this->getParams('passold');
        $passnew = $this->getParams('passnew');

        $data_user = new DataUser();
        $user = $data_user->getUserInfoById(Request::session('uid'));
        if(DbUser::PASSWORD_LDAP == $user['password']){
            throw new Exception('error.admin.not_support_change_password');
        }
        if(sha1(sha1($passold).$user['salt']) !== $user['password']) {
            throw new Exception('error.admin.pass_old_error');
        }

		$salt = uniqid();
		$newPassword = sha1(sha1($passnew).$salt);
        $data_user->saveUserInfo(array('password'=>$newPassword, 'salt'=>$salt), Request::session('uid'));

        $this->response['msg'] = '密码修改成功';
    }

    /**
     * @name 校验用户名是否存在
     * @throws \Base\Exception\Controller
     */
	public function checkNameExistAction() {
		$name = $this->getParams('name');

		if((new DataUser())->isExistsByUserName($name)) {
            $error_config = \S\Config::confError('admin.user_already_exists');
            $msg = sprintf($error_config['msg'], $name);
            throw new Exception($msg,$error_config['retcode']);
		}
	}

    /**
     * @name 保存用户信息
     * @throws Exception
     * @throws \Base\Exception\Controller
     */
    public function saveUserInfoAction() {
        $uid = (int)$this->getParams('uid');
        $status = (int)$this->getParams('status')?:0;
        $isadmin = (int)$this->getParams('isadmin');
        $uname = $this->getParams('uname');
        $phone = $this->getParams('phone');
        $nick = $this->getParams('nick');
        $password = $this->getParams('password');
        $description = $this->getParams('description');
        $verify = $this->getParams('verify');

        if (!preg_match('/^[.\w]{3,20}$/', $uname)) {
            throw new Exception('error.admin.name_of_the_correct_format');
        }

        $data_user = new DataUser();
        $user_info = $data_user->getUserInfoById($uid);

        if (($user_info && $user_info['uname'] != $uname || empty($user_info)) && $data_user->isExistsByUserName($uname)) {
            $error_config = \S\Config::confError('admin.user_already_exists');
            $msg = sprintf($error_config['msg'], $uname);
            throw new Exception($msg,$error_config['retcode']);
        }
		if (strlen($nick) > 30 || empty($nick)) {
            throw new Exception('error.admin.name_of_the_correct_format');
		}
        if ($password && !preg_match('/^\w{6,18}$/', $password)) {
            throw new Exception('error.admin.password_of_the_correct_format');
        }
        if($user_info['password'] == DbUser::PASSWORD_LDAP && !$verify && !$password){
            throw new Exception('error.admin.password_of_the_correct_format');
        }
		if (!preg_match('/^1[345789]\d{9}$/', $phone)) {
            throw new Exception('error.admin.phone_of_the_correct_format');
		}

        (new DataUser())->setUserInfo($uid, $phone, $nick, $isadmin, $status, $uname, $description, $verify, $password);
        $this->response['msg'] = '信息保存成功';
    }

    /**
     * @name 禁用/解禁用户
     * @throws Exception
     */
	public function setBanAction() {
		$isban = (int)$this->getParams('isban');
		$uid = (int)$this->getParams('uid');
        if($uid == Request::session('uid')) {
            throw new Exception('error.admin.can_not_disable_yourself');
        }

        (new DataUser())->saveUserInfo(array('status'=>$isban), $uid);
        $this->response['msg'] = '设置成功';
	}

    /**
     * @name 用户列表
     */
	public function indexAction() {
        $this->response['users'] = (new DataUser())->getUserList();
	}
}
